package com.tom.joke.activity.selectimg;

import android.app.Activity;
import android.content.Intent;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;

/**
 * 图片选择的链式调用中间类。公开类。
 */
public class ImageSelector {
    private int mMaxCount = 9;
    private int mMode = SelectImageActivity.MODE_MULTI;
    private boolean mShowCamera = true;
    private ArrayList<String> mOriginData;

    private ImageSelector() {
    }

    public static ImageSelector create() {
        return new ImageSelector();
    }

    public ImageSelector single() {
        mMode = SelectImageActivity.MODE_SINGLE;
        return this;
    }

    public ImageSelector multi() {
        mMode = SelectImageActivity.MODE_MULTI;
        return this;
    }

    public ImageSelector count(int count) {
        mMaxCount = count;
        return this;
    }

    public ImageSelector showCamera(boolean showCamera) {
        mShowCamera = showCamera;
        return this;
    }

    public ImageSelector origin(ArrayList<String> originList) {
        mOriginData = originList;
        return this;
    }

    // 6.0 需要自己申请权限。
    public void start(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, SelectImageActivity.class);
        addParamsByIntent(intent);
        activity.startActivityForResult(intent, requestCode);
    }

    // 6.0 需要自己申请权限。
    public void start(Fragment fragment, int requestCode) {
        Intent intent = new Intent(fragment.getContext(), SelectImageActivity.class);
        addParamsByIntent(intent);
        fragment.startActivityForResult(intent, requestCode);
    }

    // 给intent 添加数据。
    private void addParamsByIntent(Intent intent) {
        intent.putExtra(SelectImageActivity.EXTRA_SHOW_CAMERA, mShowCamera);
        intent.putExtra(SelectImageActivity.EXTRA_SELECT_COUNT, mMaxCount);
        intent.putExtra(SelectImageActivity.EXTRA_SELECT_MODE, mMode);
        if (mOriginData != null && mMode == SelectImageActivity.MODE_MULTI) {
            intent.putStringArrayListExtra(SelectImageActivity.EXTRA_DEFAULT_SELECTED_LIST, mOriginData);
        }
    }

}
